package com.onpositive.dataminer.rcp.utils;

import java.util.ArrayList;
import java.util.Collection;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Event;

import com.onpositive.commons.elements.AbstractUIElement;
import com.onpositive.commons.elements.Container;
import com.onpositive.commons.elements.SWTEventListener;
import com.onpositive.commons.ui.appearance.HorizontalLayouter;
import com.onpositive.commons.ui.appearance.OneElementOnLineLayouter;
import com.onpositive.commons.ui.dialogs.InputElementDialog;
import com.onpositive.semantic.model.api.expressions.IEditableExpression;
import com.onpositive.semantic.model.binding.Binding;
import com.onpositive.semantic.model.binding.BindingStack;
import com.onpositive.semantic.model.binding.IBinding;
import com.onpositive.semantic.model.ui.generic.DisposeBindingListener;
import com.onpositive.semantic.model.ui.property.editors.ButtonSelector;
import com.onpositive.semantic.model.ui.property.editors.ViewerTextElement;
import com.onpositive.semantic.ui.core.Point;

public class PropertyInferenceHelper {
	
	@SuppressWarnings("rawtypes")
	public void newFormula() {
		IBinding caller = BindingStack.getCaller();
		final IEditableExpression<?> binding = (IEditableExpression<?>) caller
				.getParent().getParent().getBinding("Formulas");
		Object value = binding.getValue();
		if (!(value instanceof ArrayList)){
			ArrayList<String>v=new ArrayList<String>();
			if(value instanceof Collection){
				Collection col = (Collection) value;
				for(Object o : col){
					if(o==null)
						continue;
					
					v.add(o.toString());
				}
			}
			else{				
				v.add((String) value);				
			}
			value=v;
		}
		final ArrayList valuee = (ArrayList) value;
		ViewerTextElement ts = new ViewerTextElement();
		final Binding b = new Binding("") {
			@Override
			public void commit() {
				ArrayList<String> v = new ArrayList<String>(valuee);
				v.add((String) getValue());
				binding.setValue(v);
				
			}
		};
		ts.setBinding(b);
		ts.getLayoutHints().setHint(new Point(200,100));
		ts.setWrapText(true);
		Container cm = new Container();
		cm.setLayoutManager(new OneElementOnLineLayouter());
		cm.add(ts);
		Container element = new Container();
		element.setLayoutManager(new HorizontalLayouter());
		cm.add(element);
		cm.getLayoutHints().setMinimumSize(new Point(200,200));
		DisposeBindingListener.linkBindingLifeCycle(b, cm);
		final InputElementDialog l = new InputElementDialog(b, cm, "Formula",
				"Please enter formula");
		ButtonSelector element2 = new ButtonSelector();
		element2.addListener(SWT.Selection, new SWTEventListener<Button>() {

			@Override
			public void handleEvent(AbstractUIElement<Button> element,
					Event event) {
				l.close();
			}
		});
		element2.setText("Cancel");
		ButtonSelector element3 = new ButtonSelector();
		element3.addListener(SWT.Selection, new SWTEventListener<Button>() {

			@Override
			public void handleEvent(AbstractUIElement<Button> element,
					Event event) {
				b.commit();
				l.close();
			}
		});
		element3.setText("Add");
		element.add(element2);
		element.add(element3);
		l.open();
	}

}
